unit UExecCmdDetailAdd;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UExecCmDetailAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, System.ImageList,
  Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons, Vcl.ExtCtrls, cxSpinEdit,
  cxMaskEdit, cxDropDownEdit, cxRadioGroup, cxGroupBox, cxCheckGroup,
  cxTextEdit, cxLabel;

type
  TfrmExecCmdDetailAdd = class(TfrmExecCmdDetailAddEditBase)
    procedure btnSaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure grpOutputPropertiesChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmExecCmdDetailAdd: TfrmExecCmdDetailAdd;

implementation

uses
  UCommonConst, UCommonFunSqlite;

{$R *.dfm}

procedure TfrmExecCmdDetailAdd.btnSaveClick(Sender: TObject);
var
  sSql, sOutput: string;
  iIOID, iDetailID, iDelay, iSwitchState, iSeq: Integer;
begin
  inherited;

  // devio
  if comDevIO.ItemIndex = -1 then
  begin
    MsgBoxDefault(RESDEVIONOSELECT);
    comDevIO.SetFocus;
    Abort;
  end;

  iIOID := Integer(comDevIO.Properties.Items.Objects[comDevIO.ItemIndex]);

  iDelay := edtDelay.Value;

  // input port
  sOutput := Trim(edtOutput.Text);

  if sOutput.IsEmpty then
  begin
    MsgBoxDefault(RESEXECCMDOUTPUTPORTEMPTY);
    grpOutput.SetFocus;
    Abort;
  end;

  // switchstate
  iSwitchState := grpSwitchState.ItemIndex;

  iSeq := getMaxRecordNum('ExecCmdDetail', 'Seq') + 1;

  iDetailID := getMaxRecordNum('ExecCmdDetail', 'DetailID') + 1;

  sSql := 'insert into ExecCmdDetail (DetailID,Delay,OutputPort,SwitchState,DevIOID,MasterID) values ('
        + QuotedStr(IntToStr(iDetailID)) + ','
        + QuotedStr(IntToStr(iDelay)) + ','
        + QuotedStr(sOutput) + ','
        + QuotedStr(IntToStr(iSwitchState)) + ','
         + QuotedStr(IntToStr(iIOID)) + ','
        + QuotedStr(IntToStr(FSelectID))
        + ')';

  if ExecSql(sSql) then
  begin
    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);
end;

procedure TfrmExecCmdDetailAdd.FormShow(Sender: TObject);
begin
  inherited;

  //
end;

procedure TfrmExecCmdDetailAdd.grpOutputPropertiesChange(Sender: TObject);
begin
  inherited;

  //
end;

end.
